<template>
  <div class="dealer-management">
    <div class="header">
      <h2>经销商管理</h2>
      <div class="header-right">
        <el-select 
          v-model="filterType" 
          placeholder="全部经销商" 
          class="filter-select" 
          clearable
          @change="handleFilterChange"
        >
          <el-option label="图书经销商" value="book" />
          <el-option label="文具经销商" value="goods" />
        </el-select>
        <el-button type="primary" @click="showAddDialog">添加经销商</el-button>
      </div>
    </div>

    <el-table :data="dealers" style="width: 100%" v-loading="loading">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="经销商名称" />
      <el-table-column prop="phone" label="联系电话" />
      <el-table-column prop="type" label="经销商类型">
        <template #default="{ row }">
          {{ row.type === 'book' ? '图书经销商' : '文具经销商' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button size="small" @click="editDealer(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteDealer(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog 
      :title="dialogType === 'add' ? '添加经销商' : '编辑经销商'" 
      v-model="dialogVisible"
    >
      <el-form :model="dealerForm" label-width="100px">
        <el-form-item label="经销商名称" required>
          <el-input v-model="dealerForm.name" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="dealerForm.phone" />
        </el-form-item>
        <el-form-item label="经销商类型" required>
          <el-select v-model="dealerForm.type" placeholder="请选择经销商类型">
            <el-option label="图书经销商" value="book" />
            <el-option label="文具经销商" value="goods" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveDealer">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import DealerService from '@/api/dealerService'

const dealers = ref([])
const loading = ref(false)
const dialogVisible = ref(false)
const dialogType = ref('add')
const filterType = ref('')
const dealerForm = ref({
  name: '',
  phone: '',
  type: 'book'
})

// 获取经销商列表
const fetchDealers = async () => {
  loading.value = true
  try {
    console.log('Fetching dealers with filter type:', filterType.value)
    const response = await DealerService.getAllDealers(filterType.value)
    console.log('Dealers response:', response)
    dealers.value = response.data
  } catch (error) {
    console.error('Error fetching dealers:', error)
    ElMessage.error('获取经销商列表失败')
  } finally {
    loading.value = false
  }
}

// 处理筛选变化
const handleFilterChange = () => {
  console.log('Filter changed to:', filterType.value)
  fetchDealers()
}

// 显示添加对话框
const showAddDialog = () => {
  dialogType.value = 'add'
  dealerForm.value = {
    name: '',
    phone: '',
    type: 'book'
  }
  dialogVisible.value = true
}

// 显示编辑对话框
const editDealer = (dealer) => {
  dialogType.value = 'edit'
  dealerForm.value = { ...dealer }
  dialogVisible.value = true
}

// 保存经销商
const saveDealer = async () => {
  if (!dealerForm.value.name) {
    ElMessage.warning('请输入经销商名称')
    return
  }

  try {
    if (dialogType.value === 'add') {
      await DealerService.addDealer(dealerForm.value)
      ElMessage.success('添加经销商成功')
    } else {
      await DealerService.updateDealer(dealerForm.value.id, dealerForm.value)
      ElMessage.success('更新经销商成功')
    }
    dialogVisible.value = false
    fetchDealers()
  } catch (error) {
    console.error('Error saving dealer:', error)
    ElMessage.error(dialogType.value === 'add' ? '添加经销商失败' : '更新经销商失败')
  }
}

// 删除经销商
const deleteDealer = async (dealer) => {
  try {
    await ElMessageBox.confirm('确定要删除该经销商吗？', '提示', {
      type: 'warning'
    })
    
    await DealerService.deleteDealer(dealer.id)
    ElMessage.success('删除经销商成功')
    fetchDealers()
  } catch (error) {
    if (error !== 'cancel') {
      console.error('Error deleting dealer:', error)
      ElMessage.error('删除经销商失败')
    }
  }
}

onMounted(() => {
  fetchDealers()
})
</script>

<style scoped>
.dealer-management {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
}

.header-right {
  display: flex;
  gap: 16px;
  align-items: center;
}

.filter-select {
  width: 140px;
}
</style>
  